﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.security;
using System.Web;
using com.eforceglobal.crux.configuration;
using com.eforceglobal.crux.configuration.DTOs;

namespace com.eforceglobal.crux.controllers
{
    public class NavigationController
    {
        public static void OpenView(string ViewName)
        {
            ViewInformation viewInformation = CruxConfigurationManager.GetViewInformation(ViewName);
            HttpContext.Current.Response.Redirect(viewInformation.ViewUrl);
        }

        public static void OpenView(string ViewName, int id)
        {
            OpenView(ViewName, id.ToString());
        }

        public static void OpenView(string ViewName, List<KeyValuePair<string, object>> parameters)
        {
            ViewInformation viewInformation = CruxConfigurationManager.GetViewInformation(ViewName);
            string URLToRedirect = viewInformation.ViewUrl;
            if (parameters.Count>0)
            {
                URLToRedirect = URLToRedirect + "?";
                foreach (KeyValuePair<string, object> parameter in parameters)
                {
                    URLToRedirect = URLToRedirect + parameter.Key + "=" + parameter.Value.ToString() + "&";
                }
                URLToRedirect = URLToRedirect.TrimEnd('&');
            }
            HttpContext.Current.Response.Redirect(URLToRedirect);
        }

        public static void OpenView(string ViewName, string id)
        {
            ViewInformation viewInformation = CruxConfigurationManager.GetViewInformation(ViewName);
            string urlToRedirect = viewInformation.ViewUrl;
            if (viewInformation.ViewUrl.ToLower().Contains("[id]"))
            {
                urlToRedirect = urlToRedirect.Replace("[ID]", id);
                urlToRedirect = urlToRedirect.Replace("[id]", id);
            }
            else
            {
                urlToRedirect = viewInformation.ViewUrl + "?id=" + id;
            }
            HttpContext.Current.Response.Redirect(urlToRedirect);
        }

        

        public static void OpenErrorPage(string ErrCodeOrName)
        {
            //WishList: Don't Hard Code Error. Make The Error View Configurable!
            OpenView("Error", ErrCodeOrName);
        }

        public static string GetSectionName(string ViewName)
        {
            ViewInformation viewInformation = CruxConfigurationManager.GetViewInformation(ViewName);
            return viewInformation.ViewSection;
        }
    }
}
